/**
 * 
 */
package wayic.http;

import java.net.URL;
import java.util.Date;

/**
 * Implementation of ContentHeader.
 * 
 * @author Ashesh Nishant
 *
 */
public class HttpContentHeader implements ContentHeader {

	private String etag;
	private long lastModified;
	private int statusCode;
	private URL url;
	
	/**
	 * Default Constructor. Defaults lastModified to -1 and etag to null. 
	 */
	public HttpContentHeader(){
		this(null);
	}

	/**
	 * This is the recommended constructor.
	 * Request URL to construct a header. 
	 * @param url
	 */
	public HttpContentHeader(URL url){
	    this(null, UNINITIALIZED);
	    this.url = url;
	}
	
	/**
	 * Partial constructor.	
	 * @param etag
	 * @param lastModified
	 */
	public HttpContentHeader(String etag, long lastModified){
		this(etag, lastModified, UNINITIALIZED_STATUS);
		this.etag = etag;
		this.lastModified = lastModified;
	}
	
	/**
	 * Full constructor.	
	 * @param etag
	 * @param lastModified
	 */
	public HttpContentHeader(String etag, long lastModified, int statusCode){
		this.etag = etag;
		this.lastModified = lastModified;
		this.statusCode = statusCode;
	}
	
	/* (non-Javadoc)
	 * @see wayic.feed.elem.ContentHeaderI#getEtag()
	 */
	public String getEtag() {
		return etag;
	}

	/* (non-Javadoc)
	 * @see wayic.feed.elem.ContentHeaderI#getLastModified()
	 */
	public long getLastModified() {
		return lastModified;
	}
	
	/* (non-Javadoc)
	 * @see wayic.feed.elem.ContentHeaderI#getStatusCode()
	 */
	public int getStatusCode(){
		return statusCode;
	}

	/* (non-Javadoc)
	 * @see wayic.feed.elem.ContentHeaderI#setEtag(java.lang.String)
	 */
	public void setEtag(String etag) {
		this.etag = etag;
	}

	/* (non-Javadoc)
	 * @see wayic.feed.elem.ContentHeaderI#setLastModified(long)
	 */
	public void setLastModified(long lastModified) {
		this.lastModified = lastModified;
	}
	
	/* (non-Javadoc)
	 * @see wayic.feed.elem.ContentHeaderI#setStatusCode(int)
	 */
	public void setStatusCode(int status){
		this.statusCode = status;
	}

	
	
	/**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((etag == null) ? 0 : etag.hashCode());
        result = prime * result + (int) (lastModified ^ (lastModified >>> 32));
        result = prime * result + statusCode;
        result = prime * result + ((url == null) ? 0 : url.hashCode());
        return result;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        HttpContentHeader other = (HttpContentHeader) obj;
        if (etag == null) {
            if (other.etag != null)
                return false;
        } else if (!etag.equals(other.etag))
            return false;
        if (lastModified != other.lastModified)
            return false;
        if (statusCode != other.statusCode)
            return false;
        if (url == null) {
            if (other.url != null)
                return false;
        } else if (!url.equals(other.url))
            return false;
        return true;
    }

    /* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString(){
		return new StringBuffer(64)
		.append("\n")
		.append("***********CONTENT_HEADER start****************\n")
		.append("url                : ").append(url).append("\n")
		.append("etag               : ").append(etag).append("\n")
		.append("lastModified       : ").append(new Date(lastModified)).append("\n")
		.append("lastModified(ms)   : ").append(lastModified).append("\n")
		.append("***********CONTENT_HEADER end******************\n")
		.toString();
	}

    /**
     * Request URL for the Content.
     * @see wayic.http.ContentHeader#getUrl()
     */
    @Override
    public URL getUrl() {
        return this.url;
    }

    /**
     * Request URL for the Content.
     * @param url the url to set
     */
    public void setUrl(URL url) {
        this.url = url;
    }

}
